package jhunter.web.mvc.form;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jhunter.common.exception.ConfigException;
import jhunter.template.TemplateLoader;
import jhunter.web.mvc.ActionContext;

import org.apache.commons.lang.StringUtils;

import schema.ext.ModuleExt;
import schema.vo.module.EasyForm;

public class VelocityFormRender implements FormRender {

	@Override
	public void render(String formId, HttpServletRequest request, HttpServletResponse response) {
		
		ActionContext context = ActionContext.getContext();
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("params", context.getParameters());
		values.put("session", context.getSession());
		values.put("application", context.getApplication());
		values.put("flash", context.getFlash());
		Map<String, Object> atts = context.getAttributes();
		if (atts != null) {
			for (String key : atts.keySet()) {
				values.put(key, atts.get(key));
			}
		}
		String extendTemplate = context.getActionConfig().getExtendTemplateFilePath();
		String appendTemplate = context.getActionConfig().getAppendTemplateFilePath();

		values.put("extend_file", extendTemplate);
		values.put("append_file", appendTemplate);
		// TODO
		ModuleExt module = context.getActionConfig().getModuleExt();
		EasyForm form = module.getFormByName(formId);
		values.put("easyForm", form);
		values.put("actionExt", context.getActionConfig());
		prepareCommonVars(values, request, response);
		if (form == null) {
			throw new ConfigException("can not fount a form named :" + formId);
		}

		if (StringUtils.isNotEmpty(extendTemplate)) {
			TemplateLoader.renderByTemplate(response, "/jhunter/form/easy-form-extend-page.vm", values);
		} else {
			if (form.getType().equals("detail-form")) {
				TemplateLoader.renderByTemplate(response, "/jhunter/form/easy-form-detail.vm", values);
			} else if (form.getType().equals("edit-form")) {
				TemplateLoader.renderByTemplate(response, "/jhunter/form/easy-form-edit.vm", values);
			} else if (form.getType().equals("new-form")) {
				TemplateLoader.renderByTemplate(response, "/jhunter/form/easy-form-new.vm", values);
			} else if (form.getType().equals("list-form")) {
				TemplateLoader.renderByTemplate(response, "/jhunter/form/easy-form-list.vm", values);
			} else if (form.getType().equals("free-form")) {
				TemplateLoader.renderByTemplate(response, context.getActionConfig().getTemplateFile(form.getName()), values);
			} else if (form.getType().equals("tree-form")) {
				TemplateLoader.renderByTemplate(response, "/jhunter/form/easy-form-tree.vm", values);
			} else {
				throw new ConfigException("不支持的form type");
			}
		}

	}

	protected void prepareCommonVars(Map<String, Object> vars, HttpServletRequest request, HttpServletResponse response) {
		vars.put("contextPath", request.getContextPath());
	}
}
